<template>
  <view class="custom-rule-page">
    <!-- 头部 -->
    <view class="header">
      <navigator url="/pages/settings/settings" class="back-btn">
        <text class="iconfont icon-back"></text>
      </navigator>
      <text class="title">自定义规则</text>
      <navigator url="/pages/help/help" class="help-btn">
        <text class="help-text">帮助</text>
      </navigator>
    </view>

    <!-- 规则表单 -->
    <view class="form-container">
      <view class="form-item">
        <text class="label">标签</text>
        <input v-model="rules.tag.before" placeholder="前" class="input-before" />
        <input v-model="rules.tag.after" placeholder="后" class="input-after" />
      </view>

      <view class="form-item">
        <text class="label">快递电话</text>
        <input v-model="rules.phone.before" placeholder="前" class="input-before" />
        <input v-model="rules.phone.after" placeholder="后" class="input-after" />
      </view>

      <view class="form-item">
        <text class="label">取件码</text>
        <input v-model="rules.code.before" placeholder="前" class="input-before" />
        <input v-model="rules.code.after" placeholder="后" class="input-after" />
      </view>

      <view class="form-item">
        <text class="label">取件地址</text>
        <input v-model="rules.address.before" placeholder="前" class="input-before" />
        <input v-model="rules.address.after" placeholder="后" class="input-after" />
      </view>
    </view>

    <!-- 短信输入框 -->
    <view class="input-area">
      <textarea
        v-model="smsContent"
        placeholder="请输入或粘贴短信内容，查看解析结果，以便检查规则是否有效"
        class="sms-input"
        auto-height
      ></textarea>
    </view>

    <!-- 解析结果展示 -->
    <view class="result-area">
      <text class="result-title">解析结果：</text>
      <view class="result-box">
        <view class="result-line">
          <text class="result-label">标签：</text>
          <text>{{ parsedResult.tag }}</text>
        </view>
        <view class="result-line">
          <text class="result-label">快递电话：</text>
          <text>{{ parsedResult.phone }}</text>
        </view>
        <view class="result-line">
          <text class="result-label">取件码：</text>
          <text>{{ parsedResult.code }}</text>
        </view>
        <view class="result-line">
          <text class="result-label">取件地址：</text>
          <text>{{ parsedResult.address }}</text>
        </view>
      </view>
    </view>

    <!-- 按钮区域 -->
    <view class="button-group">
      <button class="btn-reset" @click="resetRules">重置</button>
      <button class="btn-use" @click="useRules">使用</button>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      rules: {
        tag: { before: '', after: '' },
        phone: { before: '', after: '' },
        code: { before: '', after: '' },
        address: { before: '', after: '' }
      },
      smsContent: '',
      parsedResult: {
        tag: '',
        phone: '',
        code: '',
        address: ''
      }
    };
  },
  methods: {
    // 实时解析短信内容
    parseSms() {
      try {
        const content = this.smsContent;
        if (!content) return;

        const result = {
          tag: '',
          phone: '',
          code: '',
          address: ''
        };

        // 标签：前后匹配
        if (this.rules.tag.before && this.rules.tag.after) {
          const start = content.indexOf(this.rules.tag.before);
          const end = content.indexOf(this.rules.tag.after, start);
          if (start !== -1 && end !== -1) {
            result.tag = content.substring(start + this.rules.tag.before.length, end);
          }
        }

        // 快递电话
        if (this.rules.phone.before && this.rules.phone.after) {
          const start = content.indexOf(this.rules.phone.before);
          const end = content.indexOf(this.rules.phone.after, start);
          if (start !== -1 && end !== -1) {
            result.phone = content.substring(start + this.rules.phone.before.length, end);
          }
        }

        // 取件码
        if (this.rules.code.before && this.rules.code.after) {
          const start = content.indexOf(this.rules.code.before);
          const end = content.indexOf(this.rules.code.after, start);
          if (start !== -1 && end !== -1) {
            result.code = content.substring(start + this.rules.code.before.length, end);
          }
        }

        // 取件地址
        if (this.rules.address.before && this.rules.address.after) {
          const start = content.indexOf(this.rules.address.before);
          const end = content.indexOf(this.rules.address.after, start);
          if (start !== -1 && end !== -1) {
            result.address = content.substring(start + this.rules.address.before.length, end);
          }
        }

        this.parsedResult = result;
      } catch (error) {
        console.error('解析短信时出错:', error);
      }
    },

    resetRules() {
      uni.showModal({
        title: '确认重置',
        content: '确定要重置所有规则吗？',
        success: (res) => {
          if (res.confirm) {
            this.rules = {
              tag: { before: '', after: '' },
              phone: { before: '', after: '' },
              code: { before: '', after: '' },
              address: { before: '', after: '' }
            };
            this.smsContent = '';
            this.parsedResult = {
              tag: '',
              phone: '',
              code: '',
              address: ''
            };
          }
        }
      });
    },

    useRules() {
      // 添加防重复点击机制
      if (this.isSaving) {
        return;
      }
      
      this.isSaving = true;
      
      try {
        uni.setStorageSync('customRules', this.rules);
        uni.showToast({
          title: '规则已保存',
          icon: 'success'
        });
        
        // 延迟返回，确保提示显示
        setTimeout(() => {
          this.isSaving = false;
          uni.navigateBack();
        }, 1500);
      } catch (error) {
        this.isSaving = false;
        uni.showToast({
          title: '保存失败',
          icon: 'none'
        });
        console.error('保存规则时出错:', error);
      }
    }
  },
  watch: {
    smsContent: {
      handler(newVal) {
        this.parseSms();
      },
      immediate: true
    },
    rules: {
      handler(newVal) {
        this.parseSms();
      },
      deep: true
    }
  },
  onLoad() {
    try {
      const savedRules = uni.getStorageSync('customRules');
      if (savedRules) {
        this.rules = savedRules;
      }
      this.isSaving = false;
    } catch (error) {
      console.error('读取保存的规则时出错:', error);
    }
  }
};
</script>

<style scoped>
.custom-rule-page {
  background-color: #f5f5f5;
  color: #333;
  font-size: 16px;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20rpx 30rpx;
  background-color: #f7c86a;
  color: #333;
  position: relative;
}

.back-btn, .help-btn {
  color: #333;
  font-size: 32rpx;
}

.title {
  font-size: 36rpx;
  font-weight: bold;
}

.form-container {
  padding: 20rpx;
  background-color: #fff;
  margin-bottom: 20rpx;
}

.form-item {
  display: flex;
  flex-direction: column;
  margin-bottom: 30rpx;
}

.label {
  font-size: 28rpx;
  margin-bottom: 10rpx;
  color: #333;
}

.input-before,
.input-after {
  width: 200rpx;
  height: 60rpx;
  border: 1px solid #999;
  border-radius: 8rpx;
  padding: 10rpx;
  margin-right: 20rpx;
  font-size: 24rpx;
}

.input-after {
  margin-right: 0;
}

.input-area {
  padding: 20rpx;
  background-color: #fff;
  margin-bottom: 20rpx;
}

.sms-input {
  width: 100%;
  height: 200rpx;
  border: 1px solid #999;
  border-radius: 12rpx;
  padding: 20rpx;
  font-size: 28rpx;
  line-height: 1.5;
  background-color: #fff;
}

.result-area {
  padding: 20rpx;
  background-color: #fff;
  margin-bottom: 20rpx;
}

.result-title {
  font-size: 28rpx;
  margin-bottom: 10rpx;
  color: #333;
}

.result-box {
  border: 1px solid #999;
  border-radius: 12rpx;
  padding: 20rpx;
  background-color: #fff;
  min-height: 120rpx;
  font-size: 28rpx;
  line-height: 1.5;
}

.result-line {
  display: flex;
  margin-bottom: 10rpx;
}

.result-label {
  width: 180rpx;
  font-weight: bold;
}

.button-group {
  display: flex;
  justify-content: space-between;
  padding: 30rpx;
  gap: 30rpx;
}

.btn-reset,
.btn-use {
  width: 300rpx;
  height: 80rpx;
  border-radius: 40rpx;
  font-size: 32rpx;
  color: #fff;
  background-color: #8b5a2b;
}
</style>